<template>
  <div class="user-center">
    <NavBar>
      <template #default> 我的 </template>
    </NavBar>

    <div class="user-info-wrapper" @click="handleUserInfoClick">
      <div class="user-info">
        <div class="avatar">
          <img
            v-if="userStore.userInfo && userStore.userInfo.avatar"
            :src="userStore.userInfo.avatar"
            alt="用户头像"
          />
          <el-icon v-else>
            <User />
          </el-icon>
        </div>
        <div
          v-if="userStore.userInfo && userStore.userInfo.userName"
          class="login-text"
        >
          <span>{{ userStore.userInfo.userName }}</span>
        </div>
        <div v-else class="login-text">
          <span>点击登录</span>
        </div>
        <div class="arrow">
          <el-icon>
            <ArrowRight />
          </el-icon>
        </div>
      </div>
    </div>

    <div class="order-section">
      <div class="section-header">
        <h3>我的订单</h3>
        <div class="view-all" @click="viewAllOrders">
          <span>全部</span>
          <el-icon>
            <ArrowRight />
          </el-icon>
        </div>
      </div>
      <div class="order-types">
        <div class="order-item" @click="goToOrdersByStatus(1)">
          <el-icon>
            <Money />
          </el-icon>
          <span>待付款</span>
        </div>
        <div class="order-item" @click="goToOrdersByStatus(2)">
          <el-icon>
            <Box />
          </el-icon>
          <span>待发货</span>
        </div>
        <div class="order-item" @click="goToOrdersByStatus(3)">
          <el-icon>
            <Van />
          </el-icon>
          <span>待收货</span>
        </div>
        <div class="order-item" @click="goToOrdersByStatus(5)">
          <el-icon>
            <ChatLineRound />
          </el-icon>
          <span>待评价</span>
        </div>
        <div class="order-item" @click="goToOrdersByStatus(7)">
          <el-icon>
            <Service />
          </el-icon>
          <span>退款/售后</span>
        </div>
      </div>
    </div>

    <div class="settings-section">
      <div class="setting-item" @click="goToAddress">
        <div class="item-left">
          <el-icon>
            <Location />
          </el-icon>
          <span>收货地址</span>
        </div>
        <el-icon>
          <ArrowRight />
        </el-icon>
      </div>
      <router-link to="/customerService" class="setting-item">
        <div class="item-left">
          <el-icon>
            <Headset />
          </el-icon>
          <span>客服反馈</span>
        </div>
        <el-icon>
          <ArrowRight />
        </el-icon>
      </router-link>
      <div class="setting-item" @click="goToSettings">
        <div class="item-left">
          <el-icon>
            <Setting />
          </el-icon>
          <span>通用设置</span>
        </div>
        <el-icon>
          <ArrowRight />
        </el-icon>
      </div>
      <div class="setting-item" @click="goToAccountSecurity">
        <div class="item-left">
          <el-icon>
            <User />
          </el-icon>
          <span>账号与安全</span>
        </div>
        <el-icon>
          <ArrowRight />
        </el-icon>
      </div>
      <div class="setting-item" @click="goToAbout">
        <div class="item-left">
          <el-icon>
            <InfoFilled />
          </el-icon>
          <span>关于</span>
        </div>
        <el-icon>
          <ArrowRight />
        </el-icon>
      </div>
    </div>

    <div class="logout-btn" @click="handleLogout">退出登录</div>
  </div>
</template>

<script setup>
import NavBar from "../../components/common/NavBar.vue";
import {
  ArrowRight,
  Money,
  Box,
  Van,
  Headset,
  ChatLineRound,
  Service,
  Location,
  Setting,
  InfoFilled,
  User,
} from "@element-plus/icons-vue";
import { onMounted } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
import { useUserStore } from "@/stores/user";
import { useRouter } from "vue-router"; // 引入 useRouter

const userStore = useUserStore();
const router = useRouter(); // 获取 router 实例

onMounted(() => {});

// 处理用户信息区域的点击事件
const handleUserInfoClick = () => {
  if (userStore.userInfo && userStore.userInfo.userName) {
    // 如果已登录，跳转到账号详情
    router.push('/person_message');
    ElMessage.info("跳转到个人详情页面"); // 可以保留或移除此提示
  } else {
    // 如果未登录，跳转到登录页面
    router.push('/login');
  }
};

const handleLogout = () => {
  ElMessageBox.confirm("确定要退出登录吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      userStore.logout();
      ElMessage.success("账号已退出");
      location.reload();
    })
    .catch(() => {
      ElMessage.info("已取消退出");
    });
};

const viewAllOrders = () => {
  router.push('/orders');
};

// 根据订单状态跳转到订单页面
const goToOrdersByStatus = (status) => {
  router.push({
    path: '/orders',
    query: { status: status }
  });
};
const goToAddress = () => {
  ElMessage.info("跳转到收货地址管理");
  router.push('/address'); // 实际使用时启用
};
const goToSettings = () => {
  ElMessage.info("跳转到通用设置");
  // router.push('/settings'); // 实际使用时启用
};
const goToAccountSecurity = () => {
  // 这个函数现在由 handleUserInfoClick 调用，但如果你想保留一个单独的入口，也可以留着
  ElMessage.info("跳转到账号与安全设置");
  router.push('/person_message');
};
const goToAbout = () => {
  ElMessage.info("跳转到关于页面");
  // router.push('/about'); // 实际使用时启用
};
</script>

<style scoped>
/* 保持原有样式，但请注意 .link 类已不再需要，因为我们移除了 <router-link> 标签 */
.user-center {
  background-color: #f5f5f5;
  min-height: 100vh;
  padding-bottom: 60px;
}

/* 用户信息栏 - 将 .link 的样式合并到 .user-info-wrapper 或直接应用到 .user-info */
.user-info-wrapper { /* 新增的包装 div，用于统一点击事件 */
  text-decoration: none; /* 移除 router-link 带来的下划线 */
  cursor: pointer; /* 提示可点击 */
  background-color: #fff;
  margin-bottom: 10px;
}

.user-info {
  display: flex;
  align-items: center;
  padding: 20px 15px;
  /* background-color: #fff;  已移动到 user-info-wrapper */
  /* margin-bottom: 10px; */ /* 已移动到 user-info-wrapper */
}

.avatar {
  border-radius: 50%;
  overflow: hidden;
  margin-right: 15px;
  border: 1px solid #ccc;
  height: 60px;
  width: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.avatar .el-icon {
  font-size: 60px;
  color: #666;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.login-text {
  flex: 1;
  font-size: 16px;
  color: #333;
}

.arrow {
  color: #999;
}

/* 我的订单 */
.order-section {
  background-color: #fff;
  margin-bottom: 10px;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.section-header h3 {
  margin: 0;
  font-size: 16px;
  color: #333;
}

.view-all {
  display: flex;
  align-items: center;
  color: #999;
  font-size: 14px;
  cursor: pointer;
}

.view-all span {
  margin-right: 5px;
}

.order-types {
  display: flex;
  justify-content: space-around;
  text-align: center;
}

.order-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  padding: 5px 0;
  cursor: pointer;
}

.order-item .el-icon {
  font-size: 24px;
  margin-bottom: 5px;
  color: #ff5000;
}

.order-item span {
  font-size: 12px;
  color: #666;
}

/* 设置项 */
.settings-section {
  background-color: #fff;
  margin-bottom: 10px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.setting-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  border-bottom: 1px solid #f5f5f5;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
}

.item-left {
  display: flex;
  align-items: center;
}

.item-left .el-icon {
  margin-right: 10px;
  color: #ff5000;
}

.setting-item:last-child {
  border-bottom: none;
}

/* 退出登录 */
.logout-btn {
  margin: 20px 15px;
  padding: 12px 0;
  background-color: #ff5000;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.logout-btn:hover {
  background-color: #e64a00;
}
</style>